#include <stdio.h>
#include <stdlib.h> 
#include <string.h>
#include <sys/types.h>
#include <fcntl.h> 
#include <ctype.h>  
#include <unistd.h>
#include <time.h>

#include "matchers.h"


#define MAX_REC_LEN 512

Matchers *APPLI1_Matchers;
Tags *tag;
Indexs *indexs;
Fields *fields;
pcre *pcre_pattern;

//////////////////////////////////////////////////////////////////
Tags *Tags_init()
{
	Tags *Tags = malloc(sizeof(*Tags));
	
	Tags->first = NULL;
	Tags->next = NULL;
	
	return Tags;
}

void Tags_free(Tags *Tags)
{
   	Tag *current ;
   	while (Tags->first != NULL)
   	{
      		current = Tags->first->next;
		free (Tags->first);
		Tags->first = current;	
   	}
	printf("\n-----Tags memory freed \n");
}

void Add_Tag(Tags *Tags, char *label)
{
  	
	Tag *new ; 
  	if ((new = (Tag *) malloc (sizeof (Tag))) == NULL) 
    	{
        	exit(EXIT_FAILURE);
    	} 
  	if ((new->label = (char *) malloc (50 * sizeof (char))) == NULL) 
    	{
        	exit(EXIT_FAILURE);
    	}
  	
	new->label = label; 

  	new->next = Tags->first; 
  	Tags->first = new; 
 
}
//////////////////////////////////////////////////////////////////
Indexs *Indexs_init()
{

	Indexs *Indexs = malloc(sizeof(*Indexs));
	
	Indexs->first = NULL;
	Indexs->next = NULL;
	
	return Indexs;

}

void Indexs_free(Indexs *Indexs)
{ 
	Index *current ;
   	while (Indexs->first != NULL)
   	{
      		current = Indexs->first->next;
		free (Indexs->first);
		Indexs->first = current;  		
   	}
	printf("\n-----Indexs memory freed \n");
}

void Add_Indexs(Indexs *Indexs, char *name, int priority)
{

	Index *new ; 
  	if ((new = (Index *) malloc (sizeof (Index))) == NULL) 
    	{
        	exit(EXIT_FAILURE);
    	} 
  	if ((new->name = (char *) malloc (50 * sizeof (char))) == NULL) 
    	{
        	exit(EXIT_FAILURE);
    	}
  	
	new->name = name;
	new->priority = priority; 

  	new->next = Indexs->first; 
  	Indexs->first = new;

}
//////////////////////////////////////////////////////////////////
Fields *Fields_init()
{
	Fields *Fields = malloc(sizeof(*Fields));
	
	Fields->first = NULL;
	Fields->next = NULL;
	
	return Fields;

}

void Fields_free(Fields *Fields)
{
	Field *current ;
   	while (Fields->first != NULL)
   	{
      		current = Fields->first->next;
		free (Fields->first);
		Fields->first = current;	
   	}
	printf("\n-----Fields memory freed \n");
}

void Add_Fields(Fields *Fields, char *name)
{

	Field *new ; 
  	if ((new = (Field *) malloc (sizeof (Field))) == NULL) 
    	{
        	exit(EXIT_FAILURE);
    	} 
  	if ((new->name = (char *) malloc (50 * sizeof (char))) == NULL) 
    	{
        	exit(EXIT_FAILURE);
    	}
  	
	new->name = name; 

  	new->next = Fields->first; 
  	Fields->first = new;

}
//////////////////////////////////////////////////////////////////
Matchers *Matchers_init()
{

	Matchers *Matchers = malloc(sizeof(*Matchers));
	
	Matchers->first = NULL;
	Matchers->next = NULL;
	
	return Matchers;

}
 
void Matchers_free(Matchers *Matchers)
{
	Matcher *current ;
	int i = 1;
   	while (Matchers->first != NULL)
   	{
      		current = Matchers->first->next;
		printf("\n----------------Matcher%d memory freed \n", i);
		//Tags_free(Matchers->first->Tags);
		//Indexs_free(Matchers->first->Indexs);
		//Fields_free(Matchers->first->Fields);

		free (Matchers->first);
		i++;
		Matchers->first = current;	
   	}
	printf("\n*****Matchers memory freed \n");
}
 
void Add_Matcher(Matchers *Matchers, int id, char *precheck, pcre *regex, Tags *Tags, Indexs *Indexs, Fields *Fields)
{

	Matcher *new ; 
  	if ((new = (Matcher *) malloc (sizeof (Matcher))) == NULL) 
    	{
        	exit(EXIT_FAILURE);
    	} 
  	if ((new->precheck = (char *) malloc (50 * sizeof (char))) == NULL) 
    	{
        	exit(EXIT_FAILURE);
    	}
	/*if ((new->regex = (pcre *) malloc (sizeof (pcre))) == NULL) 
    	{
        	exit(EXIT_FAILURE);
    	}*/
	if ((new->Tags = Tags_init() ) == NULL) 
    	{
        	exit(EXIT_FAILURE);
    	}

	if ((new->Indexs = Indexs_init() ) == NULL) 
    	{
        	exit(EXIT_FAILURE);
    	}
	if (( new->Fields = Fields_init() ) == NULL) 
    	{
        	exit(EXIT_FAILURE);
    	}

  	new->id = id;
	new->precheck = precheck;
	new->regex = regex;
     	new->Tags = Tags;
	new->Indexs = Indexs;
    	new->Fields = Fields;
	 

  	new->next = Matchers->first; 
  	Matchers->first = new;

}
////////////////////////////////////////////////////////////////// 
void Print_Tags(Tags *Tags)
{
	Tag *current = Tags->first;
 
	while (current != NULL)
	{ 
	        printf("label ==> %s", current->label);
	        printf("\n");

		current = current->next;
		
		printf("-----------------------------------\n");
	}
}
//////////////////////////////////////////////////////////////////
void Print_Indexs(Indexs *Indexs)
{
	Index *current = Indexs->first;
 
	while (current != NULL)
	{ 
	        printf("Index name ==> %s || ", current->name);
		printf("Priority ==> %d", current->priority);
	        printf("\n");

		current = current->next;
		
		printf("-----------------------------------\n");
	}
}
//////////////////////////////////////////////////////////////////
void Print_Fields(Fields *Fields)
{
	Field *current = Fields->first;
 
	while (current != NULL)
	{ 
	        printf("Field name ==> %s", current->name);
	        printf("\n");

		current = current->next;
		
		printf("-----------------------------------\n");
	}
}
//////////////////////////////////////////////////////////////////
void Print_Matchers(Matchers *Matchers)
{
	Matcher *current = Matchers->first;
 
	while (current != NULL)
	{

		printf(" id ==> %d \n", current->id);
		printf("precheck ==> %s \n", current->precheck);

		printf("----------------Tag----------------\n");
 
	        Print_Tags(current->Tags);

		printf("----------------Index----------------\n");
 
	     	Print_Indexs(current->Indexs);

		printf("----------------FieldS----------------\n");
 
	    	Print_Fields(current->Fields);

		current = current->next;
		
		printf("________________________________________\n");
	}
}
/////////////////////////////////////////////////////////////////////////////////////////
int T_fgets(FILE *input, char *output)
{
	fgets(output, MAX_REC_LEN, input);

	if (ferror(input))
	return 1;

	if (feof(input))
    	return 0;

  	return 1;

}
////////////////////////////////
int Timestamp( char* timestamp)
{
	time_t t;  
    	time(&t);
	int id;  
 	id = t;
	//printf("-*-*-*--*-*%d", id);
 	sprintf (timestamp ,"%s", ctime(&t) ); 
	return id;
}
